<?php
/**
 * 代金卡详情类
 * @package  htdocs
 * @subpackage  module/company
 * @author   create by allen.qiang
 * @version 1.0
 */

class cardDetail{

    /**
     * 查询现金卡
     * @param array $arg_array
     * @return array|null
     */
    public function searchCardDetail( &$arg_array ){

        global $G_SHOP;

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //卡号
        if ( isset($arg_array['card_no']) && is_numeric($arg_array['card_no']) ) {
            $sql .= ' AND card_no = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['card_no'];
        }

        //生产单ID
        if ( isset($arg_array['card_id']) && is_numeric($arg_array['card_id']) ) {
            $sql .= ' AND card_id = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['card_id'];
        }

        //入库单ID
        if ( isset($arg_array['warehouse_voucher_id']) && is_numeric($arg_array['warehouse_voucher_id']) ) {
            $sql .= ' AND warehouse_voucher_id = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['warehouse_voucher_id'];
        }

        //申领单ID
        if ( isset($arg_array['apply_id']) && is_numeric($arg_array['apply_id']) ) {
            $sql .= ' AND apply_id = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['apply_id'];
        }

        //卡面值ID
        if ( isset($arg_array['face_value_id']) && is_numeric($arg_array['face_value_id']) ) {
            $sql .= ' AND face_value_id = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['face_value_id'];
        }

        //使用的订单号
        if ( isset($arg_array['bill_no']) && is_numeric($arg_array['bill_no']) ) {
            $sql .= ' AND bill_no = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['bill_no'];
        }

        //卡状态
        if ( isset($arg_array['card_status']) && in_array( $arg_array['card_status'] , array(1,2,3,4,5,6,7) ) ) {
            $sql .= ' AND card_status = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['card_status'];
        }

        //卡类型
        if ( isset($arg_array['card_type']) && in_array( $arg_array['card_type'] , array(1,2,3) ) ) {
            $sql .= ' AND card_type = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['card_type'];
        }

        //有效期开始日期
        if (isset($_REQUEST['startDate']) && trim($_REQUEST['startDate']) != '' ) {
            $sql .= ' AND startDate >= ?';
            $params_type .= 's';
            $params_array[] = trim($_REQUEST['startDate']);
        }

        //有效期结束日期
        if (isset($_REQUEST['endDate']) && trim($_REQUEST['endDate']) != '') {
            $sql .= ' AND endDate <= ?';
            $params_type .= 's';
            $params_array[] = trim($_REQUEST['endDate']);
        }

        if (isset($arg_array['extend']) && !empty($arg_array['extend'])) {
            $sql .= $arg_array['extend'];
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM card_detail ' . $sql;
                $count_array = &$G_SHOP->DBCA->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询代金卡入库单信息
            $search_sql = 'SELECT * FROM card_detail ' . $sql;
            $temp_item_array = &$G_SHOP->DBCA->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            $item_array = array();

            foreach($temp_item_array as &$item) {
                $item_array[] = array(
                    'creator'  => $item->creator,
                    'timeStamp'=> $item->timeStamp,
                    'createTime' => $item->createTime,
                    'card_no' => $item->card_no,
                    'card_pwd' => $item->card_pwd,
                    'card_id' => $item->card_id,
                    'card_name' => $item->card_name,
                    'warehouse_voucher_id' => $item->warehouse_voucher_id,
                    'apply_id' => $item->apply_id,
                    'card_type' => $item->card_type,
                    'face_value_id' => $item->face_value_id,
                    'face_value' => $item->face_value,
                    'discount' => $item->discount,
                    'card_status' => $item->card_status,
                    'can_use_number' => $item->can_use_number,
                    'used_number' => $item->used_number,
                    'good_selector_id' => $item->good_selector_id,
                    'send_card_time' => $item->send_card_time,
                    'startDate' => $item->startDate,
                    'endDate' => $item->endDate,
                    'bill_no' => $item->bill_no
                );

            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }

}